<template>
	<view class="">
		<Header :isBack="true">
			<block slot="content">{{title}}</block>
		</Header>
		<view class="content">
			<view class="row b-b">
				<text class="tit">联系人</text>
				<input class="input" type="text" v-model="addressData.realname" placeholder="收货人姓名"
					placeholder-class="placeholder" />
			</view>
			<view class="row b-b">
				<text class="tit">手机号</text>
				<input class="input" type="number" v-model="addressData.mobile" placeholder="收货人手机号码"
					placeholder-class="placeholder" />
			</view>
			<view class="row b-b">
				<text class="tit">区域</text>
				<Region :defaultValue="defaultValue" @change="onChangeRegion"></Region>
			</view>
			<view class="row b-b">
				<text class="tit">地址</text>
				<!-- #ifdef APP-PLUS -->
				<!-- <text @click="chooseLocation" class="input">
					{{addressData.addressName}}
				</text>
				<text class="yticon icon-shouhuodizhi"></text> -->
				<input class="input" type="text" v-model="addressData.address" placeholder="地址"
					placeholder-class="placeholder" />
				<!-- #endif -->
				<!-- #ifdef H5 -->
				<input class="input" type="text" v-model="addressData.address" placeholder="地址"
					placeholder-class="placeholder" />
				<!-- #endif -->
				<!-- #ifdef MP-WEIXIN -->
				<input class="input" type="text" v-model="addressData.address" placeholder="地址"
					placeholder-class="placeholder" />
				<!-- #endif -->
			</view>
			<!-- <view class="row b-b">
				<text class="tit">门牌号</text>
				<input class="input" type="text" v-model="addressData.area" placeholder="楼号、门牌"
					placeholder-class="placeholder" />
			</view> -->

			<view class="row default-row">
				<text class="tit">设为默认</text>
				<switch :checked="addressData.default.value" color="#fa436a" @change="switchChange" />
			</view>
			<button class="add-btn" @click="confirm">提交</button>
		</view>
	</view>
</template>

<script>
	import Region from '@/components/region/region.vue';
	export default {
		data() {
			return {
				title: '',
				defaultValue: [],
				addressData: {
					id: 0,
					realname: '',
					mobile: '',
					area: '110100',
					addressName: '在地图选择',
					address: '',
					default: {
						value: false
					}
				}
			}
		},
		onLoad(option) {
			//判断是否登录
			let result = this.$api.isLogin();
			if (result === false) {
				return false;
			}

			let title = '新增收货地址';
			if (option.type === 'edit') {
				title = '编辑收货地址'

				this.addressData = JSON.parse(option.data)
				this.addressData.addressName = this.addressData.address
				this.addressData.area = this.addressData.area_id
				if (this.addressData.status == 1) {
					this.addressData.default = {
						value: true
					};
				} else {
					this.addressData.default = {
						value: false
					};
				}

				this.defaultValue = this.addressData.area_arr
			}
			this.title = title;
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},
		components: {
			Region
		},
		methods: {
			switchChange(e) {
				this.addressData.default = e.detail;
			},

			onChangeRegion(region) {
				let id = 0;
				let area = [];
				if (region[2].id > 0) {
					id = region[2].id;
					area = [region[0].name, region[1].name, region[2].name];
				} else {
					id = region[1].id;
					area = [region[0].name, region[1].name, ''];
				}
				this.defaultValue = area;
				this.addressData.area = id;
			},

			//地图选择地址
			chooseLocation() {
				uni.chooseLocation({
					success: (data) => {

						this.addressData.addressName = data.name;
						this.addressData.address = data.name;
						console.log(this.addressData)
					}
				})
			},

			//提交
			async confirm() {
				let dataObj = this.addressData;
				if (!dataObj.realname) {
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if (!/(^1[3|4|5|7|8|9][0-9]{9}$)/.test(dataObj.mobile)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (!dataObj.area) {
					this.$api.msg('请选择区域');
					return;
				}
				if (!dataObj.address) {
					this.$api.msg('请在地图选择所在位置');
					return;
				}

				dataObj.token = uni.getStorageSync('token')
				dataObj.cate = uni.getStorageSync('cate')

				let res = await this.$myRequest({
					url: '/editAddress',
					data: dataObj,
					methods: 'post'
				})

				uni.$emit('changeAddress')
				this.$api.msg(res.data.msg);
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages6/pages/address/address'
					})
				}, 800)
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		// #ifdef APP-PLUS
		padding-top: 162rpx;
		// #endif

		// #ifdef H5
		padding-top: 110rpx;
		// #endif

		// #ifdef MP-WEIXIN
		padding-top: 160rpx;
		// #endif
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;
		font-size: $font-base;

		.tit {
			flex-shrink: 0;
			width: 120upx;
			font-size: $font-base;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}

		.icon-shouhuodizhi {
			font-size: 36upx;
			color: $font-color-light;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>